<%
layout("/layouts/platform.html"){
%>
<section class="content-wrap bg-white">
    <header class="header navbar bg-white shadow">
        <div class="pull-right offscreen-right">
            <button class="btn btn-primary navbar-btn" onclick="sublime.toggleFilter('.cd-panel')"><i
                    class="fa fa-sliders"></i> 筛选
            </button>
        </div>
    </header>
    <div class=panel-body style="padding-top: 50px;">
        <div class="table-responsive no-border">
            <input id="unitid" type="hidden">
            <table class="table table-bordered table-striped mg-t datatable">
                <thead>
                <tr>
                    <th>卡券名称</th>
                    <th>使用提醒</th>
                    <th>额度</th>
                    <th>状态</th>
                    <th>已发放数量</th>
                    <th>操作</th>
            </table>
        </div>
    </div>
</section>
<div class="cd-panel from-right">
    <header class="cd-panel-header">
        <h4>高级筛选</h4>
    </header>
    <div class="cd-panel-container">
        <div class="cd-panel-content shadow">
            <div class="form-group">
                <label for="coupon_name">卡券名</label>
                <input type="text" id="coupon_name" name="coupon_name" onclick="this.value=''" class="form-control" placeholder="卡券名" autofocus>
            </div>
            <button id="searchBtn" type="button" class="btn btn-default">查询</button>
        </div>
    </div>
</div>
<!-- 投放卡券 -->
<div id="dialogPuton" class="modal fade bs-modal-sm" tabindex="-2" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">投放卡券 </h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-xs-12">
                      确定投放此卡券吗？ <br/>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取 消</button>
                <button id="okPuton" type="button" class="btn btn-primary" data-loading-text="正在投放...">确 定</button>
            </div>
        </div>
    </div>
</div>
<script language="JavaScript">
    var datatable;
    function initDatatable() {
        datatable = $('.datatable').DataTable({
            "dom": '<"toolbar">frtip',
            "searching":false,
            "processing": true,
            "serverSide": true,
            "select": true,
            "ordering": true,
            "language": {
                "url": "${base}/assets/plugins/datatables/cn.json"
            },
            "preDrawCallback": function () {
                sublime.showLoadingbar($(".main-content"));
            },
            "drawCallback": function () {
                sublime.closeLoadingbar($(".main-content"));
            },
            "ajax": {
                "url": "${base}/platform/ud/mcoupon/data",
                "type": "post",
                "data": function (d) {
                    d.coupon_name = $('#coupon_name').val();
                }
            },
            "order": [[0, "desc"]],
            "columns": [
                {"data": "coupon_name", "bSortable": true},
                {"data": "coupon_notice", "bSortable": true},
                {"data": "coupon_fee", "bSortable": true},
                {"data": "state", "bSortable": true}
            ],
            "columnDefs": [
                {
                    "render": function (data, type, row) {
                    	var html='<div class="btn-group"><button type="button" class="btn btn-default btn-xs dropdown-toggle" data-toggle="dropdown">' +
                        ' <i class="ti-settings"></i> <span class="ti-angle-down"></span></button><ul class="dropdown-menu" role="menu">' +
                        '<li><a href="${base}/platform/ud/mcoupon/detail/' + row.id + '" data-pjax >基本信息</a></li>' +
                        '<li class="divider"></li>';
                        if(row.state=="0"){
                        	html+='<li><a href="javascript:;" onclick="puton(\'' + row.id + '\')">投放</a>';
                        }
                        html+='<li class="divider"></li></ul></div>';
                        return  html;
                    },
                    "targets": 5
                },
                {
                    "render": function (data, type, row) {
                    	return getCount(row.id);
                    },
                    "targets": 4
                }
                ,
                {
                    "render": function (data, type, row) {
                    	if(data=="0"){
                        	return "<font color='blue'>未发放</font>";
                    	}else{
                    		return "<font color='red'>已投放</font>";
                    	}
                    },
                    "targets": 3
                }
            ]
        });
        datatable.on('click', 'tr', function () {
	    	$(this).addClass("s1").siblings().removeClass("s1");
	    	 $(this).find(":radio").attr("checked", true);
	    }); 
        $("#searchBtn").on('click', function () {
            datatable.ajax.reload();
        });
    }
    function puton(id) {
        var dialog = $("#dialogPuton");
        dialog.modal("show");
        dialog.find("#okPuton").unbind("click");
        dialog.find("#okPuton").bind("click", function (event) {
            var btn = $(this);
            btn.button("loading");
            $.post("${base}/platform/ud/mcoupon/putonDo/" + id, {}, function (data) {
                if (data.code == 0) {
                    datatable.ajax.reload(null,false);
                } else {
                    Toast.error(data.msg);
                }
                //重置按钮状态，关闭提示框
                btn.button("reset");
                dialog.modal("hide");
            }, "json");
        });
    }
    $(document).ready(function () {
        initDatatable();
    });

    //获取发放数量
    function getCount(id){
    	var datas=0;
    	$.ajax({
			url : "${base}/platform/ud/mcoupon/getCount",
			data : {
				id : id
			},
			type : "post",
			async : false,
			dataType : "json",
			success : function(data){
				datas= data;
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				return false;
			}
		});
    	return datas;
    	
    }
</script>
<%}%>
